#!/usr/bin/env python
from calibre.web.feeds.news import BasicNewsRecipe, classes


def absurl(url):
    if url.startswith('/'):
        return 'https://www.livelaw.in' + url


class livelaw(BasicNewsRecipe):
    title = 'Live Law'
    __author__ = 'unkn0wn'
    description = (
        'Live Law is a comprehensive legal news portal which is committed to providing accurate'
        ' and honest news about legal developments.'
    )
    no_stylesheets = True
    use_embedded_content = False
    encoding = 'utf-8'
    language = 'en_IN'
    remove_attributes = ['height', 'width', 'style']
    masthead_url = 'https://www.livelaw.in/images/logo.png'
    remove_empty_feeds = True
    remove_javascript = True
    ignore_duplicate_articles = {'title', 'url'}
    simultaneous_downloads = 1

    extra_css = '''
        .news_detail_person_detail {font-size:small; color:#202020;}
        .news-description { color:#202020; font-style:italic; }
    '''

    keep_only_tags = [
        dict(name='div', attrs={'id':'page-content-wrapper'})
    ]

    remove_tags = [
        classes(
            'in-image-ad-wrap news_details_social_media_icons news_details_social_icon_desktop '
            'audioSection news_details_tags_row nextpage'
        ),
        dict(attrs={'class':lambda x: x and 'inside-post-ad' in x}),
        dict(attrs={'id':[
            'news_buzz_updates', 'after_tags', 'comments_before', 'comments', 'comments_after'
        ]})
    ]

    def preprocess_html(self, soup):
        for img in soup.findAll('img', attrs={'data-src':True}):
            img['src'] = img['data-src']
        for h2 in soup.findAll(['h2', 'h6']):
            h2.name = 'p'
        return soup

    def parse_index(self):
        index = 'https://www.livelaw.in/'
        sections = [
            'top-stories', 'supreme-court', 'high-court', 'news-updates', 'consumer-cases', 'articles',
            'lawschool', 'law-firms', 'round-ups'
        ]
        feeds = []
        soup = self.index_to_soup(index)
        for sec in sections:
            section = sec.capitalize()
            self.log(section)
            articles = []
            for a in soup.findAll('a', attrs={'href':lambda x: x and x.startswith('/' + sec + '/')}):
                url = absurl(a['href'].split('?')[0])
                if url in {index + sec + '/', index + sec}:
                    continue
                if not url[-1].isdigit():
                    continue
                title = self.tag_to_string(a)
                self.log('\t', title, '\n\t\t', url)
                articles.append({'title': title, 'url': url})
            if articles:
                feeds.append((section, articles))
        return feeds
